package cn.tedu.yushougeproduct.controller;

import cn.tedu.yushouge.commons.web.JsonResult;
import cn.tedu.yushougeproduct.pojo.param.CartSkuItemUpdateInfoParam;
import cn.tedu.yushougeproduct.pojo.param.ShoppingCartItemAddParam;
import cn.tedu.yushougeproduct.pojo.vo.AttributeStandardVO;
import cn.tedu.yushougeproduct.pojo.vo.ShoppingCartItemListVO;
import cn.tedu.yushougeproduct.service.ICartSkuItemService;
import cn.tedu.yushougeproduct.service.impl.CartSkuItemServiceImpl;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/cartItem/")
@Slf4j
@Api(tags = "05：购物车项管理模块")
@Validated
public class CartSkuItemController {

    @Autowired
    private ICartSkuItemService cartSkuItemService;

    @PostMapping("add-new")
    @ApiOperation("添加购物车项")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid ShoppingCartItemAddParam shoppingCartItemAddParam) {
        log.debug("开始处理【添加购物车项】的请求，参数：{}", shoppingCartItemAddParam);
        cartSkuItemService.addNew(shoppingCartItemAddParam);
        return JsonResult.ok();
    }

    @GetMapping("deleteById")
    @ApiOperation("删除购物车项")
    @ApiOperationSupport(order = 200)
    public JsonResult<Void> deleteById(@RequestParam Long id){
        log.debug("开始处理【根据ID删除购物车项】的请求，购物车项ID：{}", id);
        cartSkuItemService.deleteByID(id);
        return JsonResult.ok();
    }

    @GetMapping("update-quantity")
    @ApiOperation("修改购物车项")
    @ApiOperationSupport(order = 200)
    public JsonResult<Void> updateQuantity(@RequestParam Long id, @Valid CartSkuItemUpdateInfoParam cartSkuItemUpdateInfoParam){
        log.debug("开始处理【根据id修改购物车项数量】的请求，购物车项ID：{}", id);
        cartSkuItemService.updateQuantityByID(id,cartSkuItemUpdateInfoParam);
        return JsonResult.ok();
    }

    @GetMapping("list-by-cart")
    @ApiOperation("根据ID查询购物车项列表")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cartId", value = "购物车Id", required = true, dataType = "long")
    })
    public JsonResult<List<ShoppingCartItemListVO>> getStandardById(@Range(min = 1, message = "根据ID查询购物车项列表失败，请提交合法的ID值！")
                                                                 @RequestParam Long cartId) throws Exception {
        log.debug("开始处理【根据购物车ID查询购物车项】的请求，参数：{}", cartId);
        List<ShoppingCartItemListVO> result = cartSkuItemService.listByCartId(cartId);
        return JsonResult.ok(result);
    }
}
